<template>
  <!-- 上边距和左边距去掉，否则会导致打印时错位 -->
  <div class="app-container" style="margin-left: -20px;margin-top: -20px;" >
    <div id="printPreview"  v-html="mainPrint"></div>
  </div>
</template>

<script>
import { getPut ,print } from '@/api/wind/print/print'
export default {
  name: 'printView',
  data() {
    return {
      //图片地址前缀
      imgurl: process.env.VUE_APP_BASE_API,
      // 打印预览内容
      mainPrint: undefined,
      // 遮罩层
      loading: true,
      // 年
      yearMap: {
        "2019":"M",
        "2020":"W",
        "2021":"O",
        "2022":"T",
        "2023":"H",
        "2024":"F",
        "2025":"V",
        "2026":"X",
        "2027":"E",
        "2028":"G",
        "2029":"N",
        "2030":"R"
      },
      //月
      monthMap: {
        "01":"J",
        "02":"B",
        "03":"M",
        "04":"R",
        "05":"Y",
        "06":"N",
        "07":"L",
        "08":"G",
        "09":"P",
        "10":"T",
        "11":"V",
        "12":"C"
      },
      //日
      daysMap: {
        "01":"F",
        "02":"C",
        "03":"A",
        "04":"E",
        "05":"B",
        "06":"&",
        "07":"D",
        "08":"J",
        "09":"G",
        "10":"@",
        "11":"I",
        "12":"H",
        "13":"P",
        "14":"M",
        "15":"K",
        "16":"O",
        "17":"L",
        "18":"#",
        "19":"N",
        "20":"T",
        "21":"Q",
        "22":"R",
        "23":"S",
        "24":"$",
        "25":"Z",
        "26":"%",
        "27":"U",
        "28":"Y",
        "29":"V",
        "30":"W",
        "31":"X"
      },
      //数字
      numbenMap: {
        "0":"Z",
        "1":"O",
        "2":"T",
        "3":"R",
        "4":"F",
        "5":"V",
        "6":"X",
        "7":"S",
        "8":"E",
        "9":"N"
      },
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      imgsrc: "",
      dialogVisible:false,

    };
  },
  created() {
    let ids = JSON.parse(this.$route.params.ids);
    console.log(ids);
    if (ids) {
      let putList;
      getPut(ids).then(response => {
        putList = response.rows;
        let str = "";

        for(let i = 0;i < putList.length; i++){
          let team = putList[i].team;				//班组
          let AreaID = putList[i].areaID;			//产线
          let DefName = putList[i].defName ;		//产品名称
          let DZ = putList[i].gy_dz;				//定重
          let WinderName = putList[i].winderName;	//员工编号
          let printDate = putList[i].printdate;
          printDate = this.formatDate(printDate);

          let printdate = "";
          let printnodate = "";
          let bottomLabel = "";
          let dz = "";
          let area = "";
          let areaID = "";
          let dateList = [];
          let sn = "";

          let is_bottom = putList[i].is_bottom;		//底部描述是否打印:  1是  0否
          let is_dz = putList[i].is_dz;				//定重是否打印:  1是  0否
          let is_date = putList[i].is_date;			//日期方式:0  不打印    1 日期格式：年-月-日   2  日期格式：日-月-年
          let is_sn = putList[i].is_sn;				//顺逆区分: 0 不打印 1顺时针  2逆时针

          //处理产线
          if(AreaID == "0101304"){
            areaID = "0104";
          } else if(AreaID == "0101307"){
            areaID = "0105";
          }

          //处理班组
          if(team == "1"){
            team = "A";
          } else if(team == "2"){
            team = "B";
          } else if(team == "3"){
            team = "C";
          }

          //处理底部标签
          if(is_bottom == "1"){
            bottomLabel = "Made in China";
          }

          //处理定重
          if(is_dz == "1"){
            dz = DZ ;
          }

          //顺逆区分
          if(is_sn == "0"){
            sn = "";
          } else if(is_sn == "1"){
            sn = "CW";
          } else if(is_sn == "2"){
            sn = "CC";
          }

          //处理日期
          dateList = printDate.split("-");
          if(is_date == "0"){
            printnodate = "";
            for(let p = 0;p < dateList.length ;p++){
              if(p == 0){
                printnodate = printnodate + this.yearMap[dateList[p]] + ' ';
              } else if(p == 1){
                printnodate = printnodate + this.monthMap[dateList[p]] + ' ';
              } else if(p == 2){
                printnodate = printnodate + this.daysMap[dateList[p]] + ' ';
              }
            }
            console.log(printnodate);
          } else if(is_date == "1"){
            printdate = printDate;
          } else if(is_date == "2") {
            printdate = "";
            for (let m = dateList.length - 1; m >= 0; m--) {
              if (m == 0) {
                printdate = printdate + dateList[m];
              } else {
                printdate = printdate + dateList[m] + '-';
              }
            }
          }

          if(dz == null){
            dz = "";
          }

          this.imgsrc = process.env.VUE_APP_BASE_API +'/business/pell/qrCode/' + DefName + '?printdate=' + printdate
                        + '&dz=' + dz  + '&areaID=' + areaID  + '&team=' + team  + '&WinderName=' + WinderName

          //二维码上方文本内容
          area = areaID + team + WinderName;
          for(let j = 0;j < putList[i].printcount;j++){
            str = str + '<div class="outC">' +
              '<div class="topBQ">ASSEMBLED ROVING</div>' +
              '<div style="width:300px;height:508px;margin-top:-310px;">' +
              '<div class="DefNameC">'+DefName+'</div>' +
              '<div class="printdateC">'+printdate+'</div>' +
              '<div class="snC">'+sn+'</div>' +
              '<div class="printnodateC">'+printnodate+'</div>' +
              '<div class="DZC">'+dz+'</div>' +
              '</div>' +
              '<div style="width:100px;height:508px;">' +
              '<div class="topLabel">'+area+'</div>' +
              '<div style="margin-left: 275px;margin-top: 10px;">' +
              '<img class="imgC" src='+this.imgsrc+' /></div>' +
              '<div class="bootomMSC">'+bottomLabel+'</div>' +
              '</div>' +
              '</div>';
          }

            //打印一行结束后打印一页空白页作为区分
            str = str + '<div class="outC"><div class="topBQ">络纱</div></div>';
          }

        str = str+"</div>";

        this.mainPrint = str;
      });
    }

    clearTimeout(this.timer);  //清除延迟执行

    //打印延迟处理，等数据加载完毕后打印
    this.timer = setTimeout(()=>{   //设置延迟执行
      window.print();

      let formData = new FormData();
      formData.append("guids", JSON.stringify(ids));

      print(formData).then(response => {

        this.$router.push({ path: "/pell/windprint" });

      });

    },1000);
  },
  methods: {
    formatDate(value) {
      let date = new Date(value);
      let y = date.getFullYear();
      let MM = date.getMonth() + 1;
      MM = MM < 10 ? ('0' + MM) : MM;
      let d = date.getDate();
      d = d < 10 ? ('0' + d) : d;
      let h = date.getHours();
      h = h < 10 ? ('0' + h) : h;
      let m = date.getMinutes();
      m = m < 10 ? ('0' + m) : m;
      let s = date.getSeconds();
      s = s < 10 ? ('0' + s) : s;
      return y + '-' +MM + '-' + d ;
    },
  },

}
</script>

<style scoped>
@import "~@/assets/styles/pelle.scss";
</style>
